/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slideshow;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author Filippo
 */
public class Window extends JFrame implements WindowConstants {
    private JButton previous;
    private JButton next;
    private ImagePanel currentImagePane;
    private List<Picture> pictures;
    public Window() {
        super(Window.WINDOWS_NAME + Window.SEPARATOR_LABEL + Window.LOADING_LABEL);
        this.setBounds(Window.WINDOWS_X,Window.WINDOWS_Y,Window.WINDOWS_WIDTH + Window.WINDOWS_WIDTH_MARGIN,Window.WINDOWS_HEIGHT + Window.WINDOWS_HEIGHT_MARGIN);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel contentPane = new JPanel();
        this.previous = new JButton(Window.PREVIOUS_LABEL);
        this.next = new JButton(Window.NEXT_LABEL);
        this.currentImagePane = new ImagePanel();
        this.previous.setBounds(0,0,Window.BUTTONS_WIDTH,Window.WINDOWS_HEIGHT);
        this.next.setBounds(Window.BUTTONS_WIDTH + 2 * Window.PANEL_SPACE + Window.IMAGE_WIDTH,0,Window.BUTTONS_WIDTH,Window.WINDOWS_HEIGHT);
        this.currentImagePane.setBounds(Window.BUTTONS_WIDTH + Window.PANEL_SPACE,Window.PANEL_SPACE,Window.IMAGE_WIDTH,Window.IMAGE_HEIGHT);
        this.previous.addActionListener(new Window.PreviousImage(this));
        this.next.addActionListener(new Window.NextImage(this));
        contentPane.setLayout(null);
        contentPane.add(this.previous);
        contentPane.add(this.currentImagePane);
        contentPane.add(this.next);
        this.setContentPane(contentPane);
        this.setResizable(false);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
            System.out.println("Look and feel not supported.");
        }
        final String dirname = args[0];
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ExecuteTasks.execute(dirname);
            }
        });  
    }
    public void setPath(String dirname) throws IOException {
        this.pictures = ImageLoader.load(dirname);
        if(this.pictures.isEmpty()) {
            this.setErrorState(Window.WINDOWS_NAME + Window.SEPARATOR_LABEL + Window.NO_IMAGES_LABEL);
        }
        else {
            Picture first = this.pictures.get(0);
            this.currentImagePane.setImage(first.getImage());
            this.setTitle(Window.WINDOWS_NAME + Window.SEPARATOR_LABEL + first.getFilename());
            this.previous.setEnabled(false);
            if(this.pictures.size() < 2) {
                this.next.setEnabled(false);
            }
            this.repaint();
        }
    }
    private int currentIndex() {
        int index = 0;
        Image current = this.currentImagePane.getImage();
        searchingForImage:
        for(int i = 0 ; i < this.pictures.size() ; i++) {
            Image tmp = this.pictures.get(i).getImage();
            if(current == tmp) {
                index = i;
                break searchingForImage;
            }
        }
        return index;
    }
    public void previousImage() {
        int prevIndex = this.currentIndex() - 1;
        Picture prev = this.pictures.get(prevIndex);
        this.currentImagePane.setImage(prev.getImage());
        this.setTitle(Window.WINDOWS_NAME + Window.SEPARATOR_LABEL + prev.getFilename());
        if(prevIndex == 0) {
            this.previous.setEnabled(false);
        }
        this.next.setEnabled(true);
        this.repaint();
    }
    public void nextImage() {
        int nextIndex = this.currentIndex() + 1;
        Picture nxt = this.pictures.get(nextIndex);
        this.currentImagePane.setImage(nxt.getImage());
        this.setTitle(Window.WINDOWS_NAME + Window.SEPARATOR_LABEL + nxt.getFilename());
        if(nextIndex == this.pictures.size() - 1) {
            this.next.setEnabled(false);
        }
        this.previous.setEnabled(true);
        this.repaint();
    }
    public void setErrorState(String message) {
        this.previous.setEnabled(false);
        this.next.setEnabled(false);
        this.currentImagePane = new ImagePanel();
        this.setTitle(Window.WINDOWS_NAME + Window.SEPARATOR_LABEL + message);
        this.repaint();
    }
    private class PreviousImage implements ActionListener {
        private Window window;
        public PreviousImage(Window window) {
            this.window = window;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            this.window.previousImage();
        }        
    }
    private class NextImage implements ActionListener {
        private Window window;
        public NextImage(Window window) {
            this.window = window;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            this.window.nextImage();
        }        
    }
}
